//
//  AdviseViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/24.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "AdviseViewController.h"

#define MAXVALUE 200
#define MINVALUE 0

@interface AdviseViewController ()<UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UITextView *text;
@property (weak, nonatomic) IBOutlet UITextField *textPhone;
@property (weak, nonatomic) IBOutlet UILabel *lengLabel;

@end

@implementation AdviseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self showNav:@"建议与反馈"];
    
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self.text setContentOffset:CGPointMake(0, 0)];
}

//提交反馈与建议
- (IBAction)clickTJ:(id)sender {
    if (_text.text.length == 0) {
        [LICGToast showWithText:@"请输入建议或问题"];
        return;
    }
    
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:token forKey:@"verifyToken"];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    [parmas setObject:_text.text forKey:@"content"];
    [parmas setObject:_textPhone.text forKey:@"phone"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/Companyinfo/addProposal",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        [SVProgressHUD dismiss];
        NSLog(@"提交反馈与建议res:%@",responseObject);
        NSInteger status = [[responseObject objectForKey:@"status"] integerValue];
        NSString *msg = [responseObject objectForKey:@"msg"];
        if (status == 200) {
            
            [LICGToast showWithText:msg];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [LICGToast showWithText:msg];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}

- (void)textViewDidChange:(UITextView *)textView
{
    UITextRange *selectedRange = [textView markedTextRange];
    //获取高亮部分
    UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
    
    //如果在变化中是高亮部分在变，就不要计算字符了
    if (selectedRange && pos) {
        return;
    }
    
    NSUInteger count = textView.text.length;
    if (MINVALUE > 0 && count < MINVALUE) {
        self.lengLabel.text = [NSString stringWithFormat:@"字数不少于%d个字", MINVALUE];
    } else {
        self.lengLabel.text = [NSString stringWithFormat:@"%ld/%ld", (unsigned long)count, (long)MAXVALUE];
    }
}

//- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
//    NSInteger textlen = self.text.text.length + text.length;
//    if (textlen < 201) {
//        [self.lengLabel setText:[NSString stringWithFormat:@"%zd/200",textlen]];
//        return YES;
//    }else{
//        return NO;
//    }
//
//}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
